﻿singleApp.controller('joinCtrl', ['$http', '$scope', '$interval', function ($http, $scope, $interval) {
	$scope.vm = {
	    user_name: "",
	    phone: "",
	    sms: "",
	    sms_id: 0,
	};

	$scope.btn_could_click = true;
	$scope.remain_seconds = 60;

	$scope.init = function () {
	    var local = localStorage.getItem("had_join_2");
	    if (local != null) {	        
	        $http.get(window.singleConfig.rootUrl + "api/user/CommonService/getUser?phone=" + local).then(function (result) {
	            if (result.data.code == "success") {
	                location.href = "http://xg.pailixiang.com/album_ia328203246.html";
	            }
	        }, function () { });
	    }
	};
	$scope.init();

	$scope.getsms = function () {
	    if ($scope.vm.phone == undefined || $scope.vm.phone == "") {
	        layer.msg("请输入11位手机号 !");
	        return;
	    }

	    if ($scope.btn_could_click == false)
	        return;

	    var api_url = window.singleConfig.rootUrl + 'api/user/AuthService/smsSend';
	    layer.load(2);
	    $http({
	        method: 'POST',
	        url: api_url,
	        data: $.param($scope.vm),
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
	    })
        .then(function (result) {
            var data = result.data;
            if (data.code == "success") {
                layer.closeAll();
                layer.msg("发送成功!");
                $scope.btn_could_click = false;
                $scope.remain_seconds = 60;
                $scope.vm.sms_id = data.id;

                var obj = $interval(function () {
                    if ($scope.remain_seconds == 1) {
                        $scope.btn_could_click = true;
                        $interval.cancel(obj);
                    }
                    else {
                        $scope.remain_seconds--;
                        $("#spanRemainSeconds").html($scope.remain_seconds);
                    }
                }, 1000);
            }
            else {
                layer.closeAll();
                layer.msg(data.message);
            }
        }, function (data) {
            layer.closeAll();
            layer.msg("发送失败!");
        });
	};


    $scope.submit = function () {
        if ($scope.vm.user_name == undefined || $scope.vm.user_name == "") {
            layer.msg("请输入姓名 !");
            return;
        }

	    if ($scope.vm.phone == undefined || $scope.vm.phone == "") {
			layer.msg("请输入电话号码 !");
			return;
	    }

	    if ($scope.vm.sms == undefined || $scope.vm.sms == "") {
	        layer.msg("请输入短信验证码 !");
	        return;
	    }

	    var api_url = window.singleConfig.rootUrl + 'api/user/CommonService/join';
		layer.load(2);
		$http({
			method: 'POST',
			url: api_url,
			data: $.param($scope.vm),
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
		})
		.then(function (result) {
			var data = result.data;
			if (data.code == "success") {
			    layer.closeAll();
			    layer.msg("提交成功！");
			    localStorage.setItem("had_join_2", $scope.vm.phone);
			    setTimeout(function () {
			        location.href = "http://xg.pailixiang.com/album_ia328203246.html";
			    }, 300);
			}
			else {
				layer.closeAll();		    
				layer.msg(data.message);
			}
		},function (data) {		
			layer.msg("手机号或者密码错误!");
		});	
	}


}]);